package com.slunk.physics;

import com.badlogic.gdx.physics.box2d.Filter;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.math.Vector2;

public class SingleScreenFactory {
	private World world;
	private Body top,bottom,left,right;
	
	/*
	 * Public SingleScreenFactory for creating a screen boundary in box2d
	 */
	public SingleScreenFactory (World world, float width, float height, float thickness) {
		//Store reference of the world to derive the bodies from
		this.world = world;
		
		this.bottom = this.world.createBody(createBodyWithPosition(new Vector2(0.0f,0.0f)));
		Fixture bottomFixture = this.bottom.createFixture(createBox(width,thickness), 0.0f);
		Filter bottomFilter = new Filter();
		//bottomFilter.groupIndex = -1;
		bottomFixture.setFilterData(bottomFilter);
		
		this.top = this.world.createBody(createBodyWithPosition(new Vector2(0.0f,height)));
		Fixture topFixture = this.top.createFixture(createBox(width,thickness), 0.0f);
		Filter topFilter = new Filter();
		//topFilter.groupIndex = -1;
		topFixture.setFilterData(topFilter);
		
		this.left = this.world.createBody(createBodyWithPosition(new Vector2(0.0f,0.0f)));
		Fixture leftFixture = this.left.createFixture(createBox(thickness,height), 0.0f);
		Filter leftFilter = new Filter();
		leftFilter.groupIndex = -2;
		leftFixture.setFilterData(leftFilter);
		
		this.right = this.world.createBody(createBodyWithPosition(new Vector2(width,0.0f)));
		Fixture rightFixture =  this.right.createFixture(createBox(thickness,height),0.0f);
		Filter rightFilter = new Filter();
		rightFilter.groupIndex = -2;
		rightFixture.setFilterData(rightFilter);
	}
	
	/*
	 * Method to test if body is apart of the boundary
	 */
	public boolean isBoundary (Body body) {
		if (body == this.top ||
				body == this.bottom ||
				body == this.left ||
				body == this.right) {
			return true;
		}
		return false;
	}
	
	
	/*
	 * Create body from world
	 */
	private BodyDef createBodyWithPosition(Vector2 position) {
		BodyDef bodyDef = new BodyDef();
		bodyDef.position.set(position);
		return bodyDef;
	}
	
	/*
	 * Create Polygon shape using width and height
	 */
	private PolygonShape createBox(float width, float height) {
		PolygonShape box = new PolygonShape();
		box.setAsBox(width, height);
		return box;
	}
}
